﻿var SelectedColorFocused = "#adc3e7";
var SelectedColorUnfocused = "#eeeeee";
var _mode = 0;

//	lookup modes
//
var LookupBrowse = 0x1;
var LookupShowColumns = 0x2;
var LookupMultiSelect = 0x4;

function checkMode(mode, option) {
    return ((mode & option) == option);
}

function search() {
    document.body.style.cursor = "wait";
    var url = "map_lookupdata.aspx";

    url += window.location.search;
    url += "&Mode=" + _mode;
    //url += "&ObjectType=" + selObjects.returnValue;

    if (!checkMode(_mode, LookupBrowse)) {
        url += "&searchvalue=" + URLEncode(findValue.value);
        btnGo.disabled = true;
        findValue.focus();
    }

    frmResults.location.href = url;
}

function selectChange(o) {
    //	re-browse on selection change
    //
    if (checkMode(_mode, LookupBrowse)) {
        search();
    }

    switch (parseInt(o.returnValue, 10)) {
        case Account: btnNew.disabled = !_canCreateAccount; break;
        case Contact: btnNew.disabled = !_canCreateContact; break;
        case Lead: btnNew.disabled = !_canCreateLead; break;
        case Opportunity: btnNew.disabled = !_canCreateOpportunity; break;

        default:

            btnNew.disabled = true;

            break;
    }
}

function buildReturnValue(rows) {
    var lookupItems = new LookupItems();

    var len = rows.length;

    for (var i = 0; i < len; i++) {
        var tr = rows[i];
        var columns = tr.parentElement.parentElement.getElementsByTagName("col");

        //	'None' is always undefined, don't add
        //
        if (tr.oid != undefined) {
            lookupItems.add(tr, columns)
        }
    }

    return lookupItems;
}

//	create a selected items collection on the table if it doesn't exist
//
function initSelectedItems(table) {
    if (table.selectedItems == undefined) {
        table.selectedItems = new Array();

        table.onactivate = activateItems;
        table.ondeactivate = deactivateItems;
    }
}

function getActiveItem(elem) {
    while (elem.tagName != "TR") {
        elem = elem.parentElement;
    }

    return elem;
}

function unselectItems(table) {
    var multiSelect = checkMode(_mode, LookupMultiSelect);

    if (table.selectedItems == undefined) return;
    if (multiSelect && event.shiftKey) return;
    if (multiSelect && event.ctrlKey && event.keyCode == 0) return;

    while (table.selectedItems.length > 0) {
        unselectItem(table, table.selectedItems[0]);
    }
}

function unselectItem(table, item) {
    if (table.selectedItems == undefined) return;

    var items = table.selectedItems;
    for (var i = 0; i < items.length; i++) {
        if (items[i] == item) {
            items[i].selected = 0;
            items[i].runtimeStyle.backgroundColor = "";
            table.selectedItems.splice(i, 1);
            break;
        }
    }
}

function selectItem(table, item, focused) {
    if (item.unselectable != undefined) return;

    //	init this table's selected items collection
    //
    initSelectedItems(table);

    item.selected = 1;

    if (focused)
        item.runtimeStyle.backgroundColor = SelectedColorFocused;
    else
        item.runtimeStyle.backgroundColor = SelectedColorUnfocused;

    table.lastSelected = item;

    //	add the newly selected item to the collection
    //
    table.selectedItems.push(item);

    //	disable properties btn if item in results selected without id ("none")
    //
    if (table.id == "tblResults" && "undefined" != typeof (parent.btnProperties)) {
        parent.btnProperties.disabled = (item.oid == undefined);
    }
}

function shiftSelectItems(table, o) {
    if (table.lastSelected != undefined) {
        var lastSelected = table.lastSelected;

        if (lastSelected.rowIndex >= o.rowIndex) {
            var rows = table.rows;
            for (var i = o.rowIndex; i < lastSelected.rowIndex; i++) {
                selectItem(table, rows[i], true);
            }
        }
        else {
            var rows = table.rows;

            for (var i = lastSelected.rowIndex + 1; i <= o.rowIndex; i++) {
                selectItem(table, rows[i], true);
            }
        }
    }
}

function clickItem(table) {
    if (event.srcElement.tagName == "TBODY") return;

    var item = getActiveItem(event.srcElement);
    var multiSelect = checkMode(_mode, LookupMultiSelect);

    if (multiSelect && event.shiftKey && !item.selected) {
        shiftSelectItems(table, item);
    }
    else if (multiSelect && event.ctrlKey && item.selected) {
        unselectItem(table, item, true);
    }
    else {
        unselectItems(table);

        selectItem(table, item, true);
    }
    return item;
}

function findValueKeyDown() {
    if (event.keyCode == 13) {
        search();
    }
}

//	keyboard enable the list
//
function listKeyDown(table) {
    if (event.keyCode == 32) {
        table.ondblclick();
    }
    else if (event.keyCode == 38) {
        var item = table.lastSelected;

        if (item && item.rowIndex > 0) {
            item = item.previousSibling;

            if (item.unselectable != undefined) {
                item = item.previousSibling;

                if (item == null)
                    return;
            }

            unselectItems(table);

            selectItem(table, item, true);

            //window.scrollBy(item.style.height - 1, 0);

            item.scrollIntoView(true);
        }
    }
    else if (event.keyCode == 40) {
        var item = table.lastSelected;

        if (item && item.rowIndex < table.rows.length - 1) {
            item = item.nextSibling;

            if (item.unselectable != undefined) {
                item = item.nextSibling;

                if (item == null)
                    return;
            }

            unselectItems(table);

            selectItem(table, item, true);

            //window.scrollBy((item.style.height * -1), 0);

            item.scrollIntoView(false);
        }
    }
    else if (!(event.shiftKey || event.ctrlKey) && ((event.keyCode > 47 && event.keyCode < 58) || (event.keyCode > 64 && event.keyCode < 91))) {
        var code;
        var len = table.rows.length;

        for (i = (checkMode(_mode, LookupMultiSelect)) ? 0 : 2; i < len; i++) {
            code = table.rows[i].cells[0].innerText.charCodeAt(0);

            if ((code == event.keyCode) || ((event.keyCode > 64 && event.keyCode < 91) && (code == event.keyCode + 32))) {
                item = table.rows[i];

                unselectItems(table);

                selectItem(table, item, true);

                item.scrollIntoView(true);

                break;
            }
        }
    }
}

// switch the selection focus of all items
//
function focusSelectedItems(table, focused) {
    if (table.selectedItems == undefined) return;

    var items = table.selectedItems;

    //	need to select the first item in the list if ther aren't any selected items
    //
    if (items.length == 0 && table.rows.length > 0) {
        selectItem(table, table.rows[0], true);
    }

    for (var i = 0; i < items.length; i++) {
        if (focused) {
            items[i].runtimeStyle.backgroundColor = SelectedColorFocused;
        }
        else {
            items[i].runtimeStyle.backgroundColor = SelectedColorUnfocused;
        }
    }
}

function activateItems() {
    if (!this.contains(event.toElement)) {
        focusSelectedItems(this, true);
    }
}

function deactivateItems() {
    if (!this.contains(event.toElement)) {
        focusSelectedItems(this, false);
    }
}

function showProperties() {
    var items = frmResults.tblResults.selectedItems;

    if (items.length == 0) {
        //alert("You must first select an object.");
        alert("请选择一个对象！"); //^^
    }
    else if (items.length > 1) {
        //alert("You must only select one object.");
        alert("您只能选择一个对象。"); //^^
    }
    else {
        openStdWin("/_forms/readonly/readonly.aspx?objTypeCode=" + items[0].otype + "&id=" + items[0].oid, "readonly" + buildWinName(items[0].oid), 560, 525);
    }
}

function createNew() {
    var returnObject = openStdDlg('/_forms/QuickCreateReturn/quickcreate.aspx?crmFormSubmitObjectType=' + selObjects.returnValue, selObjects.returnValue, 400, 400);

    if (returnObject) {
        findValue.value = unescape(returnObject.name);
        search();
    }
}

//	item classes
//
function LookupItem() {
    // public:
    //
    this.id = "";
    this.name = "";
    this.html = "";
    this.type = "";
    this.values = null;
    this.keyValues = null;
}

function LookupItemData(name, value) {
    // public:
    //
    this.name = name;
    this.value = value;
}

function LookupItems() {
    // public:
    //
    this.add = _add;
    this.items = new Array();

    // private:
    //
    function _add(tr, columns) {
        var li = new LookupItem();
        var td = tr.cells[0];

        li.id = tr.oid;
        li.name = td.innerText;
        li.html = td.innerHTML;
        li.type = tr.otype;
        li.values = new Array();
        li.keyValues = new Array();

        //	if there is more then one col then we need to 
        //	pack 'em up for the caller
        //
        var len = columns.length;
        if (len > 1) {
            for (var i = 1; i < len; ++i) {
                li.keyValues[new String(columns[i].name)] = new LookupItemData(columns[i].name, tr.cells[i].innerText)
                li.values.push(new LookupItemData(columns[i].name, tr.cells[i].innerText));
            }
        }

        this.items.push(li);
    }
}

// From Grid.js
function RenderToolTip() {
    var o = event.srcElement;

    if (o && o.tagName == "NOBR" && o.title == "" && o.innerText != "") {
        o.title = o.innerText;
    }
}
